//
//  BFSetPasswordViewController.m
//  BFoodSystem
//
//  Created by 浙江择富 on 2018/4/4.
//  Copyright © 2018年 陈名正. All rights reserved.
//

#import "BFSetPasswordViewController.h"
#import "BFLoginServices.h"

@interface BFSetPasswordViewController ()<UITextFieldDelegate>

@end

@implementation BFSetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.registerBtn.enabled = NO;
    self.passwordTF.delegate =self;
    self.confirmPasswordTF.delegate = self;
    
    if ([self.selfType isEqualToString:@"changPsd"]) {
        [self.registerBtn setBackgroundImage:[UIImage imageNamed:@"confirmN"] forState:UIControlStateNormal];
        [self.registerBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateDisabled];
    }
    
}
- (IBAction)registerBtn:(id)sender {
    
        if(![self.passwordTF.text isEqualToString:self.confirmPasswordTF.text]){
            [BFUtils showAlertController:1 title:@"" message:@"两次密码不一致"];
            return;
        }
    
    if ([self.selfType isEqualToString:@"changPsd"]) {
        [[BFLoginServices alloc] findPasswordWithUserName:self.userNameStr password:self.passwordTF.text smsCode:self.smsStr inviteCode:self.inviteStr SuccessBlock:^(id result) {
            [BFUtils hideProgressHUDInView:self.view delegate:self animated:YES afterDelay:0];
//            [self.loginVc setUserName:userNameStr passWordStr:firPsdStr];
            
            UIAlertController * alert = [BFUtils alertController:nil message:@"密码修改成功"];
            UIAlertAction * confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
            [alert addAction:confirm];
        } errorCode:^(NSInteger errorCode, NSString *errorMessage) {
            [BFUtils hideProgressHUDInView:self.view delegate:self animated:YES afterDelay:0];
            [BFUtils showAlertController:0 title:@"" message:errorMessage];
            
        } Failure:^(NSError *error) {
            [BFUtils hideProgressHUDInView:self.view delegate:self animated:YES afterDelay:0];
            [BFUtils showAlertController:0 title:@"" message:@"网络错误"];
        }];
    }else{
        
        [[BFLoginServices alloc] userRegistWithUserName:self.userNameStr password:self.passwordTF.text smsCode:self.smsStr inviteCode:self.inviteStr SuccessBlock:^(id result) {
            [BFUtils hideProgressHUDInView:self.view delegate:self animated:YES afterDelay:0];
            UIAlertController * alert = [BFUtils alertController:nil message:@"注册成功"];
            
            UIAlertAction * confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //                        self.loginVc.userLableTF.text = userNameStr;
                //                        self.loginVc.passwordTF.text = firPsdStr;
                [UserDefults setValue:self.userNameStr forKey:UserName];
                
                [self.navigationController popViewControllerAnimated:YES];
            }];
            [alert addAction:confirm];
            
        } errorCode:^(NSInteger errorCode, NSString *errorMessage) {
            [BFUtils hideProgressHUDInView:self.view delegate:self animated:YES afterDelay:0];
            [BFUtils showAlertController:0 title:@"" message:errorMessage];
            
        } Failure:^(NSError *error) {
            [BFUtils hideProgressHUDInView:self.view delegate:self animated:YES afterDelay:0];
            [BFUtils showAlertController:0 title:@"" message:@"网络错误"];
        }];
    }
    
    
}

#pragma mark   UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    self.registerBtn.enabled = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
